package com.inspur.dgov.bsp.system.controller;

import cn.hutool.core.util.StrUtil;
import com.inspur.dgov.bsp.system.entity.PubDict;
import com.inspur.dgov.bsp.system.service.PubDictService;
import com.inspur.dgov.bsp.system.vo.DictWebVO;
import com.inspur.dgov.common.boot.operation.RedissonCollection;
import com.inspur.dgov.common.tool.api.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

/**
 * TODO
 *
 * @Author chiyupei
 * @Date 20/3/20 下午6:19
 * @Version 1.0
 * @Description TODO
 */
@RestController
@RequestMapping("/web")
@Api(value = "开放接口", tags = "开放接口")
public class WebController {

    @Autowired
    private PubDictService pubDictService;

    @Autowired
    private RedissonCollection redissonCollection;

    /**
     * 字典接口
     */
    @GetMapping("/dict")
    @ApiOperation(value = "字典服务", notes = "传入字典类别编码和上级字典ID", position = 1)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "configCode", value = "字典类别编码", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "parentId", value = "上级字典ID", paramType = "query", dataType = "String")
    })
    public R<List<DictWebVO>> dict(@RequestParam(value = "configCode", required = true) String configCode,
                                   @RequestParam(value = "parentId", required = false) String parentId) {
        if (StrUtil.hasEmpty(configCode)) {
            return R.fail("接口调用不合法");
        }
        List<DictWebVO> list = new ArrayList<>();
        //先从Redis中获取
        List<PubDict> dictList = this.redissonCollection.getList(configCode);
        if (null != dictList && dictList.size() > 0) {
            List<PubDict> result;
            if (StrUtil.isNotBlank(parentId)) {
                result = dictList.stream().filter(item -> (item.getParentId() != null && item.getParentId().toString().equals(parentId))).collect(Collectors.toList());
            } else {
                result = dictList.stream().filter(item -> (item.getParentId() == null || item.getParentId().toString().isEmpty())).collect(Collectors.toList());
            }
            if (null != result && result.size() > 0) {
                result.sort(Comparator.comparing(PubDict::getSortOrder));
            }
            for (PubDict dict : result) {
                DictWebVO node = new DictWebVO();
                node.setId(dict.getId());
                node.setCode(dict.getCode());
                node.setName(dict.getName());
                list.add(node);
            }
        } else {
            Map<String, Object> param = new HashMap<>();
            param.put("configCode", configCode);
            param.put("parentId", parentId);
            list = this.pubDictService.webDict(param);
        }
        return R.data(list);
    }
}
